home *** CD-ROM | disk | FTP | other *** search
- #include <QuickDraw.h>
- #include <MacTypes.h>
- #include <WindowMgr.h>
- #include <TextEdit.h>
- #include <ControlMgr.h>
- #include <EventMgr.h>
-
- #include "DalDemo.h"
-
- extern int gNewWindowLeft, gNewWindowTop;
- extern int gLinesInFolder;
- extern char dirty;
- extern Rect gInRect, gInScrollBarRect, gOutRect, gOutScrollBarRect;
- extern CharsHandle gInText;
-
- /*** WindowType ***/
- int WindowType(window)
- WindowPtr window;
- {
- if (window == NIL) return(NIL_WINDOW);
-
- if (((WindowPeek)window)->windowKind < 0) return(DA_WINDOW);
-
- if (((demoPeek)window)->wType == DEMO_WINDOW) return(DEMO_WINDOW);
-
- return(UNKNOWN_WINDOW);
- }
-
- /*** CreateWindow ***/
- demoPeek CreateWindow()
- {
- Rect destRect, viewRect;
- Ptr wStore;
- WindowPtr w;
- demoPeek demoWind;
-
- wStore = NewPtr(sizeof(demoRecord));
-
- if ((w = GetNewWindow(RSRC_BASE, wStore, MOVE_TO_FRONT)) == NIL)
- ErrorHandler(NO_WIND);
- if (((screenBits.bounds.right - gNewWindowLeft) < DRAG_THRESH) ||
- ((screenBits.bounds.bottom - gNewWindowTop) < DRAG_THRESH))
- {
- gNewWindowLeft = WINDOW_HOME_LEFT;
- gNewWindowTop = WINDOW_HOME_TOP;
- }
- MoveWindow(w, gNewWindowLeft,gNewWindowTop,LEAVE_WHERE_IT_IS);
- gNewWindowLeft += NEW_WINDOW_OFFSET;
- gNewWindowTop += NEW_WINDOW_OFFSET;
-
- demoWind = (demoPeek)w;
- demoWind->wType = DEMO_WINDOW;
-
- demoWind->vScroll = NewControl( w, &gInScrollBarRect, NIL_STR, 1, 0, 0, 0,
- scrollBarProc, 0L);
- demoWind->vOutScroll = NewControl( w, &gOutScrollBarRect, NIL_STR, 1, 0, 0, 0,
- scrollBarProc, 0L);
-
- ShowWindow(w);
- SetPort(w);
- TextFont(4);
- TextSize(9);
-
- viewRect = gInRect;
- InsetRect(&viewRect, 2, 2);
- demoWind->inputTE = TENew( &viewRect, &viewRect );
- SetClikLoop(NewClikLoop, demoWind->inputTE);
- TEAutoView(TRUE, demoWind->inputTE);
-
- viewRect = gOutRect;
- InsetRect(&viewRect, 2, 2);
- demoWind->outputTE = TENew( &viewRect, &viewRect );
- SetClikLoop(NewClikLoop, demoWind->outputTE);
- TEAutoView(TRUE, demoWind->outputTE);
-
- demoWind->curTE = demoWind->inputTE;
-
- return demoWind;
- }
-
- /***NewClikLook***/
- pascal Boolean NewClikLoop()
- {
- WindowPtr window;
- demoPeek demoWind;
- TEHandle te;
- Rect tempRect;
- Point mouse;
- GrafPtr oldPort;
- int amount;
- RgnHandle oldClip;
-
- window = FrontWindow();
- if (WindowType(window) != DEMO_WINDOW) return(FALSE);
-
- demoWind = (demoPeek)window;
- te = demoWind->curTE;
-
- GetPort(&oldPort);
- SetPort(window);
- oldClip = NewRgn();
- GetClip(oldClip);
-
- SetRect(&tempRect, -32767, -32767, 32767, 32767);
- ClipRect(&tempRect);
-
- GetMouse(&mouse);
-
- if (te == demoWind->inputTE)
- {
- if (mouse.v < gInRect.top)
- {
- amount = 1;
- CommonAction(demoWind->vScroll, &amount);
- if (amount != 0)
- TEScroll(0, amount * ((*te)->lineHeight), te);
- }
- else if (mouse.v > gInRect.bottom)
- {
- amount = -1;
- CommonAction(demoWind->vScroll, &amount);
- if (amount != 0)
- TEScroll(0, amount * ((*te)->lineHeight), te);
- }
- }
- else
- {
- if (mouse.v < gOutRect.top)
- {
- amount = 1;
- CommonAction(demoWind->vOutScroll, &amount);
- if (amount != 0)
- TEScroll(0, amount * ((*te)->lineHeight), te);
- }
- else if (mouse.v > gOutRect.bottom)
- {
- amount = -1;
- CommonAction(demoWind->vOutScroll, &amount);
- if (amount != 0)
- TEScroll(0, amount * ((*te)->lineHeight), te);
- }
- }
-
- SetClip(oldClip);
- DisposeRgn(oldClip);
- SetPort(oldPort);
- return(TRUE);
- }
-
- /*** AdjustScrollBar ***/
- AdjustScrollBar(demoWind)
- demoPeek demoWind;
- {
- short value, lines, max;
- short oldValue, oldMax;
- TEPtr te;
-
- if (demoWind->curTE == demoWind->inputTE)
- {
- oldValue = GetCtlValue(demoWind->vScroll);
- oldMax = GetCtlMax(demoWind->vScroll);
- }
- else
- {
- oldValue = GetCtlValue(demoWind->vOutScroll);
- oldMax = GetCtlMax(demoWind->vOutScroll);
- }
- te = *(demoWind->curTE);
-
- lines = te->nLines;
-
- if (*(*te->hText + te->teLength - 1) == TE_CARRIAGE_RETURN)
- ++lines;
-
- max = lines - ((te->viewRect.bottom - te->viewRect.top) / te->lineHeight);
- if (max < 0) max = 0;
-
- if (demoWind->curTE == demoWind->inputTE)
- SetCtlMax(demoWind->vScroll,max);
- else
- SetCtlMax(demoWind->vOutScroll,max);
-
- te = *(demoWind->curTE);
- value = (te->viewRect.top - te->destRect.top) / te->lineHeight;
- if (value < 0) value = 0;
- else
- if (value > max) value = max;
-
- if (demoWind->curTE == demoWind->inputTE)
- {
- SetCtlValue(demoWind->vScroll, value);
- TEScroll(0, (te->viewRect.top - te->destRect.top) -
- (GetCtlValue(demoWind->vScroll) * te->lineHeight), demoWind->curTE);
- }
- else
- {
- SetCtlValue(demoWind->vOutScroll, value);
- TEScroll(0, (te->viewRect.top - te->destRect.top) -
- (GetCtlValue(demoWind->vOutScroll) * te->lineHeight), demoWind->curTE);
- }
- }
-
- /*** CommonAction***/
- void CommonAction(control,amount)
- ControlHandle control;
- short *amount;
- {
- short value, max;
-
- value = GetCtlValue(control);
- max = GetCtlMax(control);
- *amount = value - *amount;
- if (*amount < 0)
- *amount = 0;
- else if (*amount > max)
- *amount = max;
- SetCtlValue(control, *amount);
- *amount = value - *amount;
- }
-
- /*** ScrollProc ***/
- pascal void ScrollProc(theControl, theCode)
- ControlHandle theControl;
- int theCode;
- {
- int pageSize;
- int scrollAmt;
- demoPeek demoWind;
- TEPtr te;
-
- if (theCode == 0)
- return ;
-
- demoWind = (demoPeek) (*theControl)->contrlOwner;
- te = *(demoWind->curTE);
- pageSize = (te->viewRect.bottom - te->viewRect.top) / te->lineHeight;
-
- switch (theCode) {
- case inUpButton:
- scrollAmt = 1;
- break;
- case inDownButton:
- scrollAmt = -1;
- break;
- case inPageUp:
- scrollAmt = pageSize;
- break;
- case inPageDown:
- scrollAmt = -pageSize;
- break;
- }
- CommonAction(theControl, &scrollAmt);
- if (scrollAmt != 0)
- TEScroll(0, scrollAmt * te->lineHeight, demoWind->curTE);
-
- }
-
- /***DoContent***/
- DoContent(theWindow, theEvent)
- WindowPtr theWindow;
- EventRecord *theEvent;
- {
- int thePart;
- ControlHandle theControl;
- demoPeek demoWind;
- TEPtr te;
- int value;
- Point locmouse;
- Boolean shiftDown;
-
- if (WindowType(theWindow) != DEMO_WINDOW) return;
-
- demoWind = (demoPeek) theWindow;
- locmouse = theEvent->where;
- GlobalToLocal( &locmouse );
-
- if ((thePart = FindControl(locmouse, theWindow, &theControl)) != 0 &&
- theControl != NIL)
- {
- switch (thePart)
- {
- case inUpButton:
- case inDownButton:
- case inPageUp:
- case inPageDown:
- value = TrackControl(theControl, locmouse, (ProcPtr) ScrollProc);
- break;
- case inThumb:
- value = GetCtlValue(theControl);
- thePart = TrackControl(theControl, locmouse, NIL);
- if (thePart != 0)
- {
- value -= GetCtlValue(theControl);
- te = *(demoWind->curTE);
- if (value != 0)
- TEScroll( 0, value * te->lineHeight,demoWind->curTE);
- }
- break;
- }
- }
- else if (PtInRect(locmouse, &gInRect))
- {
- if (demoWind->curTE == demoWind->inputTE)
- {
- shiftDown = (theEvent->modifiers & shiftKey) != 0;
- TEClick(locmouse,shiftDown,demoWind->inputTE);
- }
- else
- {
- SwitchToNewArea(demoWind);
- TEClick(locmouse,FALSE,demoWind->inputTE);
- }
- }
- else if (PtInRect(locmouse, &gOutRect))
- {
- if (demoWind->curTE == demoWind->outputTE)
- {
- shiftDown = (theEvent->modifiers & shiftKey) != 0;
- TEClick(locmouse,shiftDown,demoWind->outputTE);
- }
- else
- {
- SwitchToNewArea(demoWind);
- TEClick(locmouse,FALSE,demoWind->outputTE);
- }
- }
- }
-
- /***SwitchToNewArea***/
- void SwitchToNewArea(demoWind)
- demoPeek demoWind;
- {
- if (demoWind->curTE == demoWind->inputTE)
- {
- TurnOffTextArea(demoWind,TE_INPUT_AREA);
- TurnOnTextArea(demoWind,TE_OUTPUT_AREA);
- /* HandleUpdate((WindowPtr)demoWind);*/
- }
- else
- {
- TurnOffTextArea(demoWind,TE_OUTPUT_AREA);
- TurnOnTextArea(demoWind,TE_INPUT_AREA);
- /* HandleUpdate((WindowPtr)demoWind);*/
- }
- }
-
- /***TurnOnTextArea***/
- void TurnOnTextArea(demoWind,whichArea)
- demoPeek demoWind;
- int whichArea;
- {
- TEPtr te;
-
- if (whichArea == TE_OUTPUT_AREA)
- {
- te = *demoWind->outputTE;
- te->viewRect.bottom = (((te->viewRect.bottom - te->viewRect.top) /
- te->lineHeight) * te->lineHeight) + te->viewRect.top;
- te->destRect.bottom = te->viewRect.bottom;
- HiliteControl(demoWind->vOutScroll,0);
- AdjustScrollBar(demoWind);
- demoWind->curTE = demoWind->outputTE;
- }
- else
- {
- te = *demoWind->inputTE;
- te->viewRect.bottom = (((te->viewRect.bottom - te->viewRect.top) /
- te->lineHeight) * te->lineHeight) + te->viewRect.top;
- te->destRect.bottom = te->viewRect.bottom;
- HiliteControl(demoWind->vScroll,0);
- AdjustScrollBar(demoWind);
- demoWind->curTE = demoWind->inputTE;
- }
- TEActivate(demoWind->curTE);
- }
-
- /***TurnOffTextArea***/
- void TurnOffTextArea(demoWind, whichArea)
- demoPeek demoWind;
- int whichArea;
- {
- if (whichArea == TE_OUTPUT_AREA)
- {
- HiliteControl(demoWind->vOutScroll,255);
- TEDeactivate(demoWind->outputTE);
- }
- else
- {
- HiliteControl(demoWind->vScroll,255);
- TEDeactivate(demoWind->inputTE);
- }
- }
-
-
-
- /***DrawDemoWind***/
- void DrawDemoWind(window)
- WindowPtr window;
- {
- FrameRect(&gInRect);
- FrameRect(&gOutRect);
- DrawControls(window);
-
- TextFont(geneva);
- TextFace(bold);
- MoveTo(gInRect.left - 40, gInRect.top + 12);
- DrawString("\pInput");
- MoveTo(gOutRect.left - 40, gOutRect.top + 12);
- DrawString("\pOutput");
-
- TextFont(monaco);
- TextFace(0);
- TEUpdate(&window->portRect, ((demoPeek)window)->inputTE);
- TEUpdate(&window->portRect, ((demoPeek)window)->outputTE);
- }
-
- /***HandleCloseWindow***/
- HandleCloseWindow(demoWind)
- demoPeek demoWind;
- {
- DALCloseLink(demoWind);
- HideWindow( (WindowPtr)demoWind );
- DisposeControl(demoWind->vScroll);
- DisposeControl(demoWind->vOutScroll);
- TEDispose(demoWind->curTE);
- CloseWindow( (WindowPtr)demoWind);
- DisposPtr(demoWind);
- }
-
- /*** HandleTEKey ***/
- HandleTEKey(c)
- char c;
- {
- WindowPtr w;
- demoPeek demoWind;
- int wType;
-
- w = FrontWindow();
- wType = WindowType(w);
-
- if (wType == DEMO_WINDOW)
- {
- demoWind = (demoPeek) w;
-
- switch (c)
- {
- case TE_TAB_CHAR:
- SwitchToNewArea(demoWind);
- break;
- case TE_ENTER_KEY:
- if (demoWind->curTE == demoWind->outputTE) break;
- TESetSelect(0,32767,demoWind->curTE);
- TECopy(demoWind->curTE);
- gInText = TEGetText(demoWind->curTE);
- DALSendExecute(demoWind);
- TEDelete(demoWind->curTE);
- AdjustScrollBar(demoWind);
- break;
- default:
- TEKey(c, demoWind->curTE) ;
- AdjustScrollBar(demoWind);
- dirty = 1;
- break;
- }
- }
- }
-
-
- /*** HandleActivate ***/
- HandleActivate(w,f)
- WindowPtr w;
- Boolean f;
- {
- demoPeek demoWind;
- int wType;
- Rect theRect;
-
- wType = WindowType(w);
- if (wType == DEMO_WINDOW )
- {
- demoWind = (demoPeek)w;
- if (f)
- {
- SetPort(demoWind);
- if (demoWind->curTE == demoWind->inputTE)
- {
- TurnOnTextArea(demoWind,TE_INPUT_AREA);
- ShowControl(demoWind->vScroll);
- }
- else
- {
- TurnOnTextArea(demoWind,TE_OUTPUT_AREA);
- ShowControl(demoWind->vOutScroll);
- }
- TEFromScrap();
- }
- else
- {
- if (demoWind->curTE == demoWind->inputTE)
- {
- TurnOffTextArea(demoWind,TE_INPUT_AREA);
- HideControl(demoWind->vScroll);
- }
- else
- {
- TurnOffTextArea(demoWind,TE_OUTPUT_AREA);
- HideControl(demoWind->vOutScroll);
- }
- ZeroScrap();
- TEToScrap();
- }
- }
- }
-
-